﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HRM.Model
{
    public class RegencyService : IEntityService<ChucVu>
    {
        public List<ChucVu> GetAll()
        {
            using (HRMEntities entities = new HRMEntities())
            {
                return entities.ChucVus.ToList();
            }
        }

        public bool Add(ChucVu pb)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                entities.ChucVus.Add(pb);
                entities.SaveChanges();
                return true;
            }
        }

        public bool Update(ChucVu pb)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                var query = entities.ChucVus.Where(x => x.MaChucVu == pb.MaChucVu).FirstOrDefault();
                if (query != null)
                {
                    
                    query.TenChuVu = pb.TenChuVu;
                    
                    entities.SaveChanges();
                    return true;
                }
                return false;
            }
        }
        public bool Delete(ChucVu pb)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                var query = entities.ChucVus.Where(x => x.MaChucVu == pb.MaChucVu).FirstOrDefault();
                if (query != null)
                {
                    entities.ChucVus.Remove(query);
                    entities.SaveChanges();
                    return true;
                }
                return false;
            }
        }


        public bool DeleteList(List<ChucVu> list)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                foreach (ChucVu cv in list)
                {
                    var query = entities.ChucVus.Where(x => x.MaChucVu == cv.MaChucVu).FirstOrDefault();
                    if (query != null)
                    {
                        entities.ChucVus.Remove(query);
                        
                    }
                    
                }
                entities.SaveChanges();
                return true;
            }
        }

        public List<ChucVu> GetPaging(int pageIndex, int pageSize)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                return entities.ChucVus.OrderBy(p => p.MaChucVu).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            }
        }

        public int GetNumberOfPages(int pageSize)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                int count = entities.ChucVus.Count();
                int pages = count / pageSize;
                if (count > pages * pageSize) return pages + 1;
                return pages;
            }
        }


        
    }
}
